package com.sunsys.hbase.createtable.example;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class CreateTable {

	
	public CreateTable() {
	}
	
	public void createTable(String tableName, String columnFamilyName) {
		
		Configuration configuration = HBaseConfiguration.create();
		HBaseAdmin hBaseAdmin = null;
		HTableDescriptor tableDescriptor = null;
		HColumnDescriptor columnDescriptor = null;
		
		try {
			hBaseAdmin = new HBaseAdmin(configuration);
			
			tableDescriptor = new HTableDescriptor(tableName);
			columnDescriptor = new HColumnDescriptor(columnFamilyName);
			columnDescriptor.setMaxVersions(3);
			tableDescriptor.addFamily(columnDescriptor);
			
			hBaseAdmin.createTable(tableDescriptor);
			
			
		}catch(ZooKeeperConnectionException e1){
			e1.printStackTrace();
		}catch(MasterNotRunningException e2) {
			e2.printStackTrace();
		}catch(IOException e3){
			e3.printStackTrace();
		}finally{
			try {
				if(hBaseAdmin != null) {
					hBaseAdmin.close();
				}
			}catch(Exception e1){
				;
			}

		}
	}
	
	public static void main(String []args) {
	
		CreateTable createTable = new CreateTable();
		
		createTable.createTable("UserTwits", "twits");
		System.out.println("Table created");
	}
}
